<?php
/**
 * Created by PhpStorm.
 * User: yellow
 * Date: 2019/6/25
 * Time: 14:05
 */

namespace app\admin\controller;
use app\admin\logic\SiteTeamLogic;
use think\facade\Request;
use app\vehicle\model\Volunteer;
use app\vehicle\model\User;
use app\vehicle\model\Site;
use app\vehicle\model\SiteTeam as SiteTeamModel;
use app\common\model\Street;
class SiteTeam extends Base
{

    private $SiteTeamLogic;

    public function __construct()
    {
        parent::__construct();
        $this->SiteTeamLogic = new SiteTeamLogic();
    }

    public function get_site(){
        $s = [];
        $site_id = $_SESSION['xm_travel']['admin_info']['site_id'];
        if($site_id){
            $s[] = ['id','<>',$site_id];
        }
        $site = Site::where($s)->field('name,id as value')->select();
        return json(['list'=>$site]);
    }

    public function get_site_team(){
        $where = [];
        $oid = $_SESSION['xm_travel']['admin_info']['oid'];
        if($oid){
            $s[] = ['id','<>',$oid];
        }
        $oid = Request::param('oid');
        $site = SiteTeamModel::where($s)->field('name,id as value')->select();
        if($oid){
            foreach($site as $k=>$v){
                if($oid==$v['value']){
                    $site[$k]['selected'] = true;
                }else{
                    $site[$k]['selected'] = false;
                }
            }
        }
        return json(['list'=>$site]);
    }

    public function bing_site(){
        $s = [];
        $site_id = $_SESSION['xm_travel']['admin_info']['site_id'];
        if($site_id){
            $s[] = ['id','<>',$site_id];
        }
        $site = Site::where($s)->select();
        $this->assign('site',$site);
        $where = [];
        $oid = $_SESSION['xm_travel']['admin_info']['oid'];
        if($oid){
            $where[] = ['id','<>',$oid];
        }
        $type_id = $site_id?0:1;
        $this->assign('type_id',$type_id);
        $site_team = SiteTeamModel::where($where)->select();
        $this->assign('site_team',$site_team);
        if($oid) {
            $info = $this->SiteTeamLogic->cateInfo($_SESSION['xm_travel']['admin_info']['oid']);
            $this->assign('info', $info['data']);
        }else{
            $info = Site::where('id','=',$site_id)->find();
            $this->assign('info', $info);
        }
        return $this->fetch();
    }

    public function export(){
        $objPHPExcel=new \PHPExcel();
        $objPHPExcel->setActiveSheetIndex(0);
// 设置列的宽度
        $objPHPExcel->getActiveSheet()->getColumnDimension('A')->setWidth(10);
        $objPHPExcel->getActiveSheet()->getColumnDimension('B')->setWidth(15);
        $objPHPExcel->getActiveSheet()->getColumnDimension('C')->setWidth(20);
        $objPHPExcel->getActiveSheet()->getColumnDimension('D')->setWidth(20);
        $objPHPExcel->getActiveSheet()->getColumnDimension('E')->setWidth(20);
        $objPHPExcel->getActiveSheet()->getColumnDimension('F')->setWidth(20);
// 设置表头
        $objPHPExcel->getActiveSheet()->SetCellValue('A1', '序号');
        $objPHPExcel->getActiveSheet()->SetCellValue('B1', '服务队名称');
        $objPHPExcel->getActiveSheet()->SetCellValue('C1', '负责人');
        $objPHPExcel->getActiveSheet()->SetCellValue('D1', '手机号码');
        $objPHPExcel->getActiveSheet()->SetCellValue('E1', '身份证');
        $objPHPExcel->getActiveSheet()->SetCellValue('F1', '志愿者人数');
        $objPHPExcel->getActiveSheet()->SetCellValue('G1', '状态');
        $objPHPExcel->getActiveSheet()->SetCellValue('H1', '是否发布');
        $objPHPExcel->getActiveSheet()->SetCellValue('I1', '注册时间');

        $where = Request::param();
        if($where['type']=='all'){
            $where['limit'] = 100000;
        }
        $res = $this->SiteTeamLogic->cateList($where, $where['page']);
        $data = $res['data'];
        $num = 2;
        foreach ($data as $k => $v) {
            $objPHPExcel->getActiveSheet()->SetCellValue('A' . $num, ($k+1));
            $objPHPExcel->getActiveSheet()->SetCellValue('B' . $num, $v['name']);
            $objPHPExcel->getActiveSheet()->SetCellValue('C' . $num, $v['realname']);
            $objPHPExcel->getActiveSheet()->SetCellValue('D' . $num, $v['mobile']);
            $objPHPExcel->getActiveSheet()->SetCellValue('E' . $num, ' '.$v['id_card']);
            $objPHPExcel->getActiveSheet()->SetCellValue('F' . $num, $v['numbers']);
            if($v['status']==1){
                $o_status='审核通过';
            }else if($v['status']==2){
                $o_status='审核不通过';
            }else{
                $o_status='审核中';
            }
            $objPHPExcel->getActiveSheet()->SetCellValue('G' . $num, $o_status);
            $objPHPExcel->getActiveSheet()->SetCellValue('H' . $num, $v['is_add']?'是':'否');
            $objPHPExcel->getActiveSheet()->SetCellValue('I' . $num, $v['create_time']);

            $num++;
        }
        $fileName = "志愿者表" . date('Y-m-d', time()) . rand(1, 1000);
        $xlsName = iconv('utf-8', 'gb2312', $fileName);
        $objPHPExcel->getActiveSheet()->setTitle('sheet'); // 设置工作表名
        $objWriter = new \PHPExcel_Writer_Excel5($objPHPExcel); //下载 excel5与excel2007
        ob_end_clean(); // 清除缓冲区,避免乱码
        header("Pragma: public");
        header("Expires: 0");
        header("Cache-Control:must-revalidate,post-check=0,pre-check=0");
        header("Content-Type:application/force-download");
        header("Content-Type:application/vnd.ms-execl;charset=UTF-8");
        header("Content-Type:application/octet-stream");
        header("Content-Type:application/download");
        header("Content-Disposition:attachment;filename=" . $xlsName . ".xls");
        header("Content-Transfer-Encoding:binary");
        $objWriter->save("php://output");
    }

    public function audit(){
        $this->assign('status',0);
        return $this->fetch('index');
    }

    public function index(){
        return $this->fetch();
    }

    public function ajaxIndex(){
        $where = Request::param();
        $articleList = $this->SiteTeamLogic->cateList($where, self::$nowPage);
        return json($articleList);
    }

    public function add(){
        $act = 'add';
        $id = Request::get('id');
        $this->assign('id',$id);
        if ($id){
            $act = 'edit';
            $info = $this->SiteTeamLogic->cateInfo($id);
            $where[] = ['team_admin','in','0,'.$id];
            $this->assign('info', $info['data']);
        }else{
            $where[] = ['team_admin','eq',0];
        }
        $this->assign('act', $act);

        $site = Site::select();
        $this->assign('site',$site);
        
        $street = Street::select();
        $this->assign('street',$street);

        $site_team = SiteTeamModel::select();
        $this->assign('site_team',$site_team);

        $list = User::where($where)->select();
        $this->assign('list',$list);
        return $this->fetch();
    }

    public function handle(){
        $data = Request::param();
        $res = $this->SiteTeamLogic->handle($data);
        return json($res);
    }

}